package com.light.mytext.utiles;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.light.mytext.R;


/**
 * Created by Monkey on 2018/1/20.
 */

public class LoadUtils {
    private static LinearLayoutManager layoutManager;
    private static GridLayoutManager layoutManager_gv;
    private boolean isLoading = false;
    public static interface WindowCallBack {
        public void doWork();
//        public void loadMore();
//        public void refush();
    }



    public static void loading(final Context context, final SwipeRefreshLayout swiperefresh, RecyclerView recycle, boolean ishorizon, final WindowCallBack callBack) {
        if (ishorizon) {
            layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
            recycle.setLayoutManager(layoutManager);
        } else {
            layoutManager = new LinearLayoutManager(context);
            recycle.setLayoutManager(layoutManager);
        }

        if (callBack != null) {
            recycle.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    int total = layoutManager.getItemCount();
                    int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
                    if (lastVisibleItem >= total - 1 && dy > 0) {
                        callBack.doWork();
                    }
                }

                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if(context==null)return;
                    switch (newState) {
                        case RecyclerView.SCROLL_STATE_DRAGGING:
//                            Glide.with(context).pauseRequests();
                            break;
                        case RecyclerView.SCROLL_STATE_SETTLING:
//                            Glide.with(context).pauseRequests();
                            break;
                        case RecyclerView.SCROLL_STATE_IDLE:
//                            Glide.with(context).resumeRequests();
                            break;
                    }
                }
            });
        }
        if (swiperefresh != null) {
            recycle.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return swiperefresh.isRefreshing();
                }
            });
        }
    }

    public static void loadingasGridview(final Context context, final SwipeRefreshLayout swiperefresh, RecyclerView recycle, int num, final WindowCallBack callBack) {
        layoutManager_gv = new GridLayoutManager(context, num);
        recycle.setLayoutManager(layoutManager_gv);
        if (callBack != null) {
            recycle.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    int total = layoutManager_gv.getItemCount();
                    int lastVisibleItem = layoutManager_gv.findLastVisibleItemPosition();
                    if (lastVisibleItem >= total - 1 && dy > 0) callBack.doWork();
                }

                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if(context==null)return;
                    switch (newState) {
                        case RecyclerView.SCROLL_STATE_DRAGGING:
//                            Glide.with(context).pauseRequests();
                            break;
                        case RecyclerView.SCROLL_STATE_SETTLING:
//                            Glide.with(context).pauseRequests();
                            break;
                        case RecyclerView.SCROLL_STATE_IDLE:
//                            Glide.with(context).resumeRequests();
                            break;
                    }
                }
            });
        }
        if (swiperefresh != null) {
            recycle.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return swiperefresh.isRefreshing();
                }
            });
        }
    }

    public static void loadingasJGridview(final Context context, RecyclerView recycle, int num) {
        layoutManager_gv = new GridLayoutManager(context, num);
        recycle.setLayoutManager(layoutManager_gv);
            recycle.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);

                }

                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if(context==null)return;
                    switch (newState) {
                        case RecyclerView.SCROLL_STATE_DRAGGING:
//                            Glide.with(context).pauseRequests();
                            break;
                        case RecyclerView.SCROLL_STATE_SETTLING:
//                            Glide.with(context).pauseRequests();
                            break;
                        case RecyclerView.SCROLL_STATE_IDLE:
//                            Glide.with(context).resumeRequests();
                            break;
                    }
                }
            });
    }

    public static void refresh(final Context context, final SwipeRefreshLayout swiperefresh, final WindowCallBack callBack) {
        swiperefresh.setColorSchemeColors(context.getResources().getColor(R.color.colorAccent));
        swiperefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                callBack.doWork();
            }
        });


    }

}
